import EventEmitter from 'events'

export default class MyEventEmitter extends EventEmitter {
    #eventName
    #listener
    // #eventEmitter = new EventEmitter();

    constructor(eventName) {
        super();
        this.#eventName = eventName

        this.setMaxListeners(0);
        // this.on(this.#eventName, listener);
        setInterval(this.#emit, 1000);
    }

    set listener(listener){
        this.#listener = listener
        this.on(this.#eventName, listener);
    }

    closeListener = (listener) => {
        // console.log(events.eventNames())
        console.log(`Listener count before off: ${this.listenerCount(this.#eventName)}`)
        // this.off(this.#eventName, listener)
        this.off(this.#eventName, this.#listener)
        console.log(`Listener count after off: ${this.listenerCount(this.#eventName)}`)
    }

    #emit = () => {
        console.log('events.emit ...')
        this.emit(this.#eventName, new Date());
    }
}
